const mqtt = require('../../utils/mqtt.js')
var client=null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
      deviceId:'000000001',
      TX:'/TX/LR90000000001',
      RX:'/RX/LR90000000001',
      message:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  getMsg(e){
      var value=e.detail.value;
      this.setData({
        TX:'/TX/LR900'+value,
        RX:'/RX/LR900'+value
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },  
  onChange(e){
    console.log(e);
    var value=e.detail.value;
    var obj={"lock":value}
    var str=JSON.stringify(obj);
    console.log(str);
    if(client==null){
      this.setData({
        message:  "请先点击连接服务\n"+this.data.message
      });
      return;
    }
    client.publish(this.data.RX, str);
    this.setData({
      message:  "向设备发送：" +str+ "\n"+this.data.message
    });
  },
  getAllMsg(){
    var str="{SQLID}";
    client.publish(this.data.RX, str);
    if(client==null){
      this.setData({
        message:  "请先点击连接服务\n"+this.data.message
      });
      return;
    }
    this.setData({
      message:  "向设备发送：" +str+ "\n"+this.data.message
    });
  },
  initMqtt() {
    var url="wxs:device.x-washer.com/mqtt";
    console.log("服务器地址："+url);
    let randomNum = Math.floor(Math.random() * 10000000)
    // 连接选项
    const options = {
      connectTimeout: 4000, // 超时时间
      // 认证信息 按自己需求填写
      clientId: 'wx'+randomNum,
      username: 'x-washer01',
      password: '123456Qwe',
    }
    this.setData({
      message:"clientId:"+options.clientId+"\n"
    })
    var that = this;
    if(!client){
      client=null
    }
    //client = mqtt.connect('wxs:www.x-washer.com/mqtt', options);
    client = mqtt.connect(url, options);
    client.on('reconnect', (error) => {
      this.setData({
        message:  "正在重连" + "\n"+this.data.message
      });
      console.log('正在重连:', error)
    });
    client.on('connect', (e) => {
      this.setData({
        message: "成功连接服务器"+ "\n"+this.data.message  
      });
      console.log('成功连接服务器111')
      //订阅一个主题
      client.subscribe(this.data.TX, {
        qos: 1
      }, function (err) {
        if (!err) {
          //client.publish(that.data.RX, 'Hello mqtt')
          that.setData({
            message:  "订阅设备的发布"+that.data.TX+"主题成功"+ "\n" +that.data.message 
          });
          console.log("订阅成功")
        }

      })
    })
    client.on('error', (error) => {
      that.setData({
        message: "连接失败"+ "\n" +that.data.message
      });
      console.log('连接失败:', error)
    })

    //监听mq的返回
    client.on('message', function (topic, message, packet) {
      // message is Buffer
      that.setData({
        message: "读取设备数据："+packet.payload.toString()+"\n"+that.data.message
      });
      console.log("packet", packet.payload.toString())
      //client.end()
    })
  }
})